/**
 * Created by IntelliJ IDEA.
 * User: ljy
 * Date: 2021/6/16
 * Time: 14:17
 */
package com.cowcode.controller;

import com.cowcode.biz.UserBiz;
import com.cowcode.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("index/")
@Controller
public class IndexController {
    @Resource
    private UserBiz userBiz;

    @RequestMapping("login/{jump}")
    public String login(@PathVariable("jump") String jump, User user, String yzm, String uksdl, Model model, HttpSession session) {
        String regex1 = "^1[3|4|5|8][0-9]\\d{8}$";
        String regex2 = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        if ("".equals(yzm) || null == yzm) {//当传来的验证码字段不为空的时候即为普通登录
            if (user.getUacc().matches(regex1)) {
                user.setUphone(user.getUacc());
                user.setUacc("");
                User u = null;
                u = userBiz.loginByPhone(user);
                if (u != null) {
                    session.setAttribute("user", u);
                    model.addAttribute("msg", "登录成功");
                } else {
                    model.addAttribute("msg", "邮箱或密码错误");
                }
                return "redirect:/jump/"+jump;
            }
            if (user.getUacc().matches(regex2)) {
                user.setUemail(user.getUacc());
                user.setUacc("");
                User u = null;
                u = userBiz.loginByEmail(user);
                if (u != null) {
                    session.setAttribute("user", u);
                    model.addAttribute("msg", "登录成功");
                } else {
                    model.addAttribute("msg", "电话号或密码错误");
                }
                return "redirect:/jump/"+jump;
            }
            User u = null;
            u = userBiz.loginByAcc(user);
            if (u != null) {
                session.setAttribute("user", u);
                model.addAttribute("msg", "登录成功");
                return "redirect:/jump/"+jump;
            }
            model.addAttribute("msg", "账号或密码错误");
            return "redirect:/jump/"+jump;
        } else { // 快速登录的方法
            String code = (String) session.getAttribute("code");
            if (code.equals(yzm)) {
                User user1 = new User();
                if(uksdl.matches(regex1)){
                    user1.setUphone(uksdl);
                } else if (uksdl.matches(regex2)){
                    user1.setUemail(uksdl);
                }
                User u = userBiz.loginByFast(user1);
                if (u != null) {
                    session.setAttribute("user",u);
                    model.addAttribute("msg", "登录成功");
                    return "redirect:/jump/"+jump;
                }
            }
            model.addAttribute("msg", "验证码错误");
            return "login";
        }
    }
    @RequestMapping("logout/{jump}")
    public String logout(Model model,HttpSession session,@PathVariable("jump") String jump){
        if(session.getAttribute("user")!=null){
            session.removeAttribute("user");
            model.addAttribute("msg","您已退出登录");
            return "redirect:/jump/"+jump;
        } else {
            model.addAttribute("msg","您还未登录，不能退出登录");
            return "redirect:/jump/"+jump;
        }
    }
    @RequestMapping(value = "checkyzm",method = RequestMethod.POST)
    @ResponseBody
    public int checkyzm(String yzm,HttpSession session){
        return yzm.equals((String)session.getAttribute("code"))?0:1;
    }
}
